//
//  PaperDetailViewController.m
//  SmileWeather
//
//  Created by lanou3g on 15/9/22.
//  Copyright (c) 2015年 陶天海. All rights reserved.
//

#import "PaperDetailViewController.h"

@interface PaperDetailViewController ()
@property (nonatomic, strong)UIImageView *imageView;
@property (nonatomic, strong)UIButton *backBtn;
@property (nonatomic, strong)UIButton *loadBtn;
@property (nonatomic, strong)UIButton *collectBtn;
@property (nonatomic, strong)UIButton *shareBtn;

@property (nonatomic, strong)UIActivityIndicatorView *act;
@end

@implementation PaperDetailViewController
@synthesize isTap;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.imageView = [[UIImageView alloc] initWithFrame:kBounds];
    [_imageView sd_setImageWithURL:[NSURL URLWithString:self.model.path]];
    _imageView.userInteractionEnabled = YES;
    [self.view addSubview:_imageView];
    //给背景加手势
    UITapGestureRecognizer *gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapImage)];
    [gesture setNumberOfTouchesRequired:1];
    [_imageView addGestureRecognizer:gesture];
    self.isTap = YES;
    
    [self setButton];
    
    
    
}
//ImageView的点击方法
- (void)tapImage{
    if (self.isTap == YES) {
     [UIView animateWithDuration:0.2 animations:^{
         self.navigationController.navigationBar.hidden = YES;
         self.backBtn.hidden = YES;
         self.loadBtn.hidden = YES;
         self.collectBtn.hidden = YES;
         self.shareBtn.hidden = YES;
     } completion:nil];
    self.isTap = NO;
    }else{
        [UIView animateWithDuration:0.2 animations:^{
            self.navigationController.navigationBar.hidden = NO;
            self.backBtn.hidden = NO;
            self.loadBtn.hidden = NO;
            self.collectBtn.hidden = NO;
            self.shareBtn.hidden = NO;
        }];
        self.isTap = YES;
    }
}
- (void)setButton{
    
    self.backBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.backBtn addTarget:self action:@selector(backBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    self.backBtn.backgroundColor = [UIColor purpleColor];
    [self.backBtn setTitle:@"返回" forState:(UIControlStateNormal)];
    self.backBtn.frame = CGRectMake((kWidth - 50)/3.5, kHeight - 80, 50 , 50);
    [_imageView addSubview:self.backBtn];
    
    self.loadBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.loadBtn addTarget:self action:@selector(loadBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    self.loadBtn.backgroundColor = [UIColor cyanColor];
    self.loadBtn.frame = CGRectMake(self.backBtn.frame.origin.x + 50, kHeight - 80, 50, 50);
    [self.loadBtn setTitle:@"保存" forState:(UIControlStateNormal)];
    [_imageView addSubview:self.loadBtn];
    
    self.collectBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.collectBtn addTarget:self action:@selector(collectBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    self.collectBtn.backgroundColor = [UIColor orangeColor];
    self.collectBtn.frame = CGRectMake(self.loadBtn.frame.origin.x + 50, kHeight - 80, 50, 50);
    [self.collectBtn setTitle:@"收藏" forState:(UIControlStateNormal)];
    [_imageView addSubview:self.collectBtn];
    
    self.shareBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [self.shareBtn addTarget:self action:@selector(shareBtnClick) forControlEvents:(UIControlEventTouchUpInside)];
    self.shareBtn.backgroundColor = [UIColor greenColor];
    self.shareBtn.frame = CGRectMake(self.collectBtn.frame.origin.x + 50, kHeight - 80, 50, 50);
    [self.shareBtn setTitle:@"分享" forState:(UIControlStateNormal)];
    [_imageView addSubview:self.shareBtn];
}
//返回按钮
- (void)backBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
//保存按钮
- (void)loadBtnClick{
    UIAlertView *aler = [[UIAlertView alloc] initWithTitle:@"确认保存" message:nil delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    aler.delegate = self;
    [aler show];
}
//保存按钮
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == 0) {
        self.act = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:(UIActivityIndicatorViewStyleWhiteLarge)];
        self.act.center = self.imageView.center;
        [_imageView addSubview:self.act];
        [self.act startAnimating];
        
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0), ^{
            UIImageWriteToSavedPhotosAlbum(_imageView.image, nil, nil, nil);
            dispatch_async(dispatch_get_main_queue(),^{
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"存储成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
                [alert show];
               [self.act stopAnimating];
            });
        });
        
    }else{
        return;
    }
}
//收藏按钮
- (void)collectBtnClick{
  UIAlertView * alertView = [[UIAlertView alloc] initWithTitle:@"提示" message:@"收藏成功" delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
        [alertView show];
    [[PlayerDataBase shareDataBase] createTables];
    [[PlayerDataBase shareDataBase] insertFortunePaperModel:self.model];
    //0.3秒后alertView消失
    [self performSelector:@selector(RemoveAlertAndAct:) withObject:alertView afterDelay:0.3];
    }
//alert消失
- (void)RemoveAlertAndAct:(UIAlertView *)alertView
{
    //alertView消失
    [alertView dismissWithClickedButtonIndex:0 animated:YES];
    
}

//分享按钮
- (void)shareBtnClick{
    
}

    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
